FROM nvidia/opengl:1.2-glvnd-devel-ubuntu20.04

ENV container docker
ENV DEBIAN_FRONTEND noninteractive


# Install locale
# ENV LANG C.UTF-8
# ENV LC_ALL C.UTF-8
# RUN apt-get update && apt-get install -y --no-install-recommends \
#     locales && \
#     echo "$LANG UTF-8" >> /etc/locale.gen && \
#     locale-gen && \
#     apt-get clean && \
#     rm -rf /var/lib/apt/lists/*

# Install systemd
RUN apt-get update && apt-get install -y \
    dbus dbus-x11 systemd && \
    apt-get clean && \
    rm -rf /var/lib/apt/lists/* &&\
    dpkg-divert --local --rename --add /sbin/udevadm &&\
    ln -s /bin/true /sbin/udevadm
# TODO maybe disable other targets: https://developers.redhat.com/blog/2014/05/05/running-systemd-within-docker-container/
# RUN systemctl disable systemd-resolved
VOLUME ["/sys/fs/cgroup"]
STOPSIGNAL SIGRTMIN+3
ENTRYPOINT [ "/home/default/start.sh" ]

# Install GNOME
# NOTE if you want plain gnome, use: apt-get install -y --no-install-recommends gnome-session gnome-terminal and remove the modification of /etc/gdm3/custom.conf
RUN apt-get update && apt-get install gnome-session gnome-terminal -y

# Install TigerVNC server
# TODO set VNC port in service file > exec command
# TODO check if it works with default config file
# NOTE tigervnc because of XKB extension: https://github.com/i3/i3/issues/1983
RUN apt-get update \
  && apt-get install -y tigervnc-common tigervnc-scraping-server tigervnc-standalone-server \
  && apt-get clean \
  && rm -rf /var/lib/apt/lists/*
# TODO fix PID problem: Type=forking would be best, but system daemon is run as root on startup
#   ERROR tigervnc@:1.service: New main PID 233 does not belong to service, and PID file is not owned by root. Refusing.
#   https://www.freedesktop.org/software/systemd/man/systemd.service.html#Type=
#   https://www.freedesktop.org/software/systemd/man/systemd.unit.html#Specifiers
#   https://wiki.archlinux.org/index.php/TigerVNC#Starting_and_stopping_vncserver_via_systemd
# -> this should be fixed by official systemd file once released: https://github.com/TigerVNC/tigervnc/pull/838
# TODO specify options like geometry as environment variables -> source variables in service via EnvironmentFile=/path/to/env
# NOTE logout will stop tigervnc service -> need to manually start (gdm for graphical login is not working)
# COPY tigervnc@.service /etc/systemd/system/tigervnc@.service
# RUN systemctl enable tigervnc@:1
# EXPOSE 5901

# Install noVNC
# TODO novnc depends on net-tools until version 1.1.0: https://github.com/novnc/noVNC/issues/1075
RUN apt-get update && apt-get install -y \
    net-tools novnc \
    && apt-get clean -y \
    && rm -rf /var/lib/apt/lists/*
RUN ln -s /usr/share/novnc/vnc_lite.html /usr/share/novnc/index.html
# TODO specify options like ports as environment variables -> source variables in service via EnvironmentFile=/path/to/env
COPY tigervnc@.service novnc.service /etc/systemd/system/
# RUN systemctl enable novnc
EXPOSE 5901 6901

# Create unprivileged user
# NOTE user hardcoded in tigervnc.service
# NOTE alternative is to use libnss_switch and create user at runtime -> use entrypoint script
ARG UID=1000
ARG USER=default
# RUN systemctl disable systemd-resolved && systemctl enable tigervnc@:1 && systemctl enable novnc && useradd ${USER} -u ${UID} -U -d /home/${USER} -m -s /bin/bash
RUN systemctl disable systemd-resolved && systemctl enable tigervnc@:1 && systemctl enable novnc && useradd ${USER} -u ${UID} -U -d /home/${USER} -m -s /bin/bash && apt-get update && apt-get install -y sudo && \
    apt-get clean && rm -rf /var/lib/apt/lists/* && \
    echo "${USER} ALL=(ALL) NOPASSWD: ALL" > "/etc/sudoers.d/${USER}" && \
    chmod 440 "/etc/sudoers.d/${USER}"
USER "${USER}"
ENV USER="${USER}" \
    HOME="/home/${USER}"
WORKDIR "/home/${USER}"

# Set up VNC
RUN mkdir -p $HOME/.vnc
COPY xstartup $HOME/.vnc/xstartup
# RUN echo "acoman" | vncpasswd -f >> $HOME/.vnc/passwd && chmod 600 $HOME/.vnc/passwd

# switch back to root to start systemd
USER root
COPY start.sh $HOME
RUN apt-get update && sudo apt-get install mesa-utils -y
COPY webots_2021b_amd64.deb $HOME
WORKDIR $HOME
RUN apt install ./webots_2021b_amd64.deb -y

COPY google-chrome-stable_current_amd64.deb $HOME
RUN apt install ./google-chrome-stable_current_amd64.deb -y

ENV NVIDIA_VISIBLE_DEVICES=all
ENV NVIDIA_DRIVER_CAPABILITIES=all

# RUN apt-get remove thunderbird totem rhythmbox brasero simple-scan gnome-mahjongg aisleriot gnome-mines cheese transmission-common gnome-orca gnome-sudoku libreoffice-common remmina -y && apt-get autoremove --purge -y

